import { message } from 'antd';

import type { Reducer, Effect } from 'umi';

import { addOrder, getOrderManageList, paymentCallBack, payMoney, wechatPay } from '@/services/orderManage';
import { getErrorMsg } from '@/util/utils';
const CURRENT_ORDER_INFO = localStorage.getItem('CURRENT_ORDER_INFO') && JSON.parse(localStorage.getItem('CURRENT_ORDER_INFO') as string);

export interface OrderManageState {
  list: [];
  total: 0;
  id: string;
  version?: any;
  currentOrder: any;
  currentData: any;
  wechatData?: any;
}

export interface OrderManageModelType {
  namespace: string;
  state: OrderManageState;
  effects: {
    list: Effect;
    add: Effect;
    pay: Effect;
    payment: Effect;
    doWechatPay: Effect;
  };
  reducers: {
    save: Reducer<OrderManageState>;
    getVersion: Reducer;
    setWechatData: Reducer;
    setCurrentOrder: Reducer;
  };
}

const OrderManageModel: OrderManageModelType = {
  namespace: 'OrderManage',
  state: {
    list: [],
    total: 0,
    id: '',
    currentOrder: {},
    currentData: [],
    wechatData: {},
  },
  effects: {
    /**
     * eg list
     * @param payload
     * @param call
     * @param put
     */
    *list({ payload }, { call, put }) {
      try {
        const result = yield call(getOrderManageList, payload);
        if (result?.data?.datas) {
          yield put({
            type: 'save',
            payload: {
              list: result.data?.datas,
              total: result?.data?.totalPage || 0,
              currentData: result.data?.datas[0],
            },
          });
        }
      } catch (error: any) {
        message.error(getErrorMsg(error));
      }
    },
    *add({ payload, callback }, { call }) {
      try {
        const res = yield call(addOrder, payload);
        callback(res);
      } catch (error) {
        console.log(error);
      }
    },
    *pay({ payload, callback }, { call }) {
      try {
        const res = yield call(payMoney, payload);
        callback(res);
      } catch (error) {
        console.log(error);
      }
    },
    *doWechatPay({ payload, callback, cbSetLoading }, { call, put }) {
      try {
        const res = yield call(wechatPay, payload);
        if (res.code===200) {
          yield put({
            type: 'setWechatData',
            payload: {
              ...CURRENT_ORDER_INFO,
              qrCode: res.data,
            },
          });
          localStorage.removeItem('WechatData');
          localStorage.setItem('WechatData', JSON.stringify({
            ...CURRENT_ORDER_INFO,
            qrCode: res.data,
          }));
          yield callback(res);
        } else {
          cbSetLoading();
          message.error(getErrorMsg(res));
        }
      } catch (error) {
        console.log(error);
      }
    },
    // 支付宝信息回调
    *payment({ payload, callback }, { call }) {
      try {
        const res = yield call(paymentCallBack, payload);
        callback(res);
      } catch (error) {
        console.log(error);
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    getVersion(state, { payload }) {
      return {
        ...state,
        version: payload,
      };
    },
    setWechatData(state, { payload }) {
      return {
        ...state,
        wechatData: payload,
      };
    },
    setCurrentOrder(state, { payload }) {
      return {
        ...state,
        currentOrder: {
          ...payload,
        },
      };
    },
  },
};
export default OrderManageModel;
